package edu.uic.kott.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.util.Log;

public class Compute {
	private static final String LOGTAG = "KOTT";
	private String currentDate;

	final Calendar cal = Calendar.getInstance();
	int year = cal.get(Calendar.YEAR);
	int month = cal.get(Calendar.MONTH);
	int date = cal.get(Calendar.DAY_OF_MONTH);
	int day = cal.get(Calendar.DAY_OF_WEEK);
	int week = cal.get(Calendar.WEEK_OF_YEAR);


	public void getStartEndOFWeek(){
		//enterWeek is week number
		//enterYear is year
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.WEEK_OF_YEAR, week);
		calendar.set(Calendar.YEAR, year);

		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); // PST`
		Date startDate = calendar.getTime();
		String startDateInStr = formatter.format(startDate);
		System.out.println("...date..."+startDateInStr);

		calendar.add(Calendar.DATE, 6);
		Date enddate = calendar.getTime();
		String endDateInString = formatter.format(enddate);
		System.out.println("...date..."+endDateInString);

		Log.i(LOGTAG,startDateInStr);
		Log.i(LOGTAG,endDateInString);
	}

	public void getTodaysDate(){
		currentDate = String.valueOf(month+1)+"/"+String.valueOf(date)+"/"+String.valueOf(year) + " " + String.valueOf(day);
		Log.i(LOGTAG,currentDate);
	}


	//takes in an integer array and returns a string array of days
	public List<String> daysIntToString(List<Integer> intDaysList) {
		List<String> stringDaysList = new ArrayList<String>();
		if(intDaysList.contains(1)){
			stringDaysList.add("Sunday");
			Log.i(LOGTAG, "Added Sunday for 1");
		}
		if(intDaysList.contains(2)){
			stringDaysList.add("Monday");
			Log.i(LOGTAG, "Added Monday for 2");
		}
		if(intDaysList.contains(3)){
			stringDaysList.add("Tuesday");
			Log.i(LOGTAG, "Added Tuesday for 3");
		}
		if(intDaysList.contains(4)){
			stringDaysList.add("Wednesday");
			Log.i(LOGTAG, "Added Wednesday for 4");
		}
		if(intDaysList.contains(5)){
			stringDaysList.add("Thursday");
			Log.i(LOGTAG, "Added Thursday for 5");
		}
		if(intDaysList.contains(6)){
			stringDaysList.add("Friday");
			Log.i(LOGTAG, "Added Friday for 6");
		}
		if(intDaysList.contains(7)){
			stringDaysList.add("Saturday");
			Log.i(LOGTAG, "Added Saturday for 7");
		}		
		return stringDaysList;
	}

	//Convert days of the week string to integer list
	public List<Integer> daysStringToInt(String classDays) {
		List<Integer> intDays = new ArrayList<Integer>();

		if(classDays.contains("Sunday")){
			intDays.add(1);
			Log.i(LOGTAG, "Added Sunday: 1");
		}
		if(classDays.contains("Monday")){
			intDays.add(2);
			Log.i(LOGTAG, "Added Monday: 2");
		}
		if(classDays.contains("Tuesday")){
			intDays.add(3);
			Log.i(LOGTAG, "Added Tuesday: 3");
		}
		if(classDays.contains("Wednesday")){
			intDays.add(4);
			Log.i(LOGTAG, "Added Wednesday: 4");
		}
		if(classDays.contains("Thursday")){
			intDays.add(5);
			Log.i(LOGTAG, "Added Thursday: 5");
		}
		if(classDays.contains("Friday")){
			intDays.add(6);
			Log.i(LOGTAG, "Added Friday: 6");
		}
		if(classDays.contains("Saturday")){
			intDays.add(7);
			Log.i(LOGTAG, "Added Saturday: 7");		
		}
		return intDays;
	}


	//returns a list of dates on which class sessions will be held from start date to end date
	public List<String> getDateList(String sDate, String eDate, String days) {

		List<Integer> daysOfWeek = daysStringToInt(days);		

		//Initialize the dates
		Calendar startDate = Calendar.getInstance();
		Calendar endDate = Calendar.getInstance();
		Date trueDate = Calendar.getInstance().getTime();

		//Set date format
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

		//Initialize date array list
		List<String> classSessions = new ArrayList<String>();

		//Parse the start and end dates
		try {
			startDate.setTime(df.parse(sDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		try {
			endDate.setTime(df.parse(eDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		//While the start date is earlier or equal to the end date
		while (startDate.compareTo(endDate) <= 0) {

			//Get the day of the week
			int dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
			Log.i("DayOfWeek", String.valueOf(dayOfWeek));

			//if this day is equal to a day in the daysOfWeek list
			if (daysOfWeek.contains(dayOfWeek)) {
				//Get date object of this day and convert to string
				trueDate = startDate.getTime();
				String classDate = df.format(trueDate);

				//Add the string of date to the array list
				Log.i("Class Date", classDate);
				classSessions.add(classDate);
			}
			else {
				//Not a match
				Log.i("Not contained", "Not contained");
			}

			//increment the start date
			startDate.add(Calendar.DATE, 1);
		}

		//Return list of strings containing class dates
		//!!!in MM/dd/yyyy format!!!
		return classSessions;
	}

	public List<String> termToDateList(String sDate, String eDate, String days){
		//list of dates from term start date to current date
		List<String> termToDateList = getDateList(sDate, eDate, days);
		return termToDateList;
	}

	public List<String> datesInWeek(String date) {
		List<String> dates = new ArrayList<String>();

		DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
		
		Date currentDate = Calendar.getInstance().getTime();
		try {
			currentDate = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar fromCurrent = Calendar.getInstance();
		fromCurrent.setTime(currentDate);
		
		int weekOfYear = fromCurrent.get(Calendar.WEEK_OF_YEAR);
		
		fromCurrent.set(Calendar.WEEK_OF_YEAR, weekOfYear);
		fromCurrent.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		
		for (int i = 0; i < 7; i++) {
			dates.add(df.format(fromCurrent.getTime()));
			fromCurrent.add(Calendar.DATE, 1);
		}
		
		return dates;
	}
	
	public double sumCountList(List<Integer> list) {
		double sum = 0;
		
		for (Integer i: list) {
			sum = sum + i;
		}
		
		return sum;
	}
	
	public double currentWeekMinusStartWeek (String sDate, String eDate) {
		double weeks = 0;
		
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
		
		Calendar start = Calendar.getInstance();
		try {
			start.setTime(df.parse(sDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int startWeek = start.get(Calendar.WEEK_OF_YEAR);
		
		Calendar end = Calendar.getInstance();
		try {
			end.setTime(df.parse(eDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int endWeek = end.get(Calendar.WEEK_OF_YEAR);
		
		weeks = endWeek - startWeek;
		
		return weeks;
	}
}
